package com.huaxin.hxcloud.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Entity.FindInfo;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.find.FindAdapter;
import com.huaxin.hxcloud.find.view.RefreshDataEvent;
import com.huaxin.hxcloud.frendcircles.FrendCircleActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hebing on 2017/5/17.
 */

public class FanUserInfoActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar      toolbar;
    @BindView(R.id.fan_userinfo_xrv)
    XRefreshView xRefreshView;
    @BindView(R.id.fan_userinfo_rv)
    RecyclerView recyclerView;

    public static final int TYPE_REFRESH = 1;

    public static final int TYPE_MORE = 0;
    List<FindInfo.QuestionListBean> questionList = new ArrayList<>();
    private FindAdapter findAdapter;
    private int         userId;
    public static int     pageNum = 0;
    private       boolean flags   = true;
    public static int     isFan   = 0;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_fan_userinfo;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(toolbar);
        toolbar.setTitle("我的关注");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new RefreshDataEvent(true));
                finish();
            }
        });
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setPullRefreshEnable(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        findAdapter = new FindAdapter(this, questionList);
        recyclerView.setAdapter(findAdapter);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                super.onRefresh(isPullDown);
                pageNum = 0;
                ApiRequsetData(TYPE_REFRESH, 0, isFan);
            }

            @Override
            public void onLoadMore(final boolean isSilence) {
                super.onLoadMore(isSilence);
                pageNum++;
                ApiRequsetData(TYPE_MORE, pageNum, isFan);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        userId = getIntent().getIntExtra("userId", 0);
        if (userId == 3 || userId == 5) {
            isFan = 1;
        } else {

        }
        if (userId == 3) {
            toolbar.setTitle("我的收藏");
        }
        if (userId == 5) {
            toolbar.setTitle("我的圈子");
        }

        onItemClickListener();
    }

    @Override
    protected void requestData() {
        super.requestData();
        ApiRequsetData(TYPE_REFRESH, 0, isFan);
        flags = false;
    }

    /**
     * @desc 监听点击事件
     * @author songxf
     * created at 2017/8/11 11:23
     */
    private void onItemClickListener() {
        findAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                final FindInfo.QuestionListBean mQuestionListBean = findAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.item_find_ll_layout://点击条目
                        Intent intent = new Intent(FanUserInfoActivity.this, FrendCircleActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("user", mQuestionListBean);
                        intent.putExtras(bundle);
                        intent.putExtra("position", position);
                        intent.putExtra("type", 0);
                        startActivity(intent);
                        break;
                }
            }
        });
    }


    private void refreshDatas(int loadtype) {
        if (loadtype == TYPE_REFRESH) {
            findAdapter.setNewData(questionList);
        } else if (loadtype == TYPE_MORE) {
            findAdapter.addData(questionList);
        }
        findAdapter.notifyDataSetChanged();
    }

    private void ApiRequsetData(final int type, int pageNum, int isFan) {
        Call<FindInfo> call = null;
        if (type == TYPE_REFRESH && flags) {
            updateLoadStatus(LOADING);
        }
        if (isFan == 1) {
            call = getRequestApi().requestFindData(userId, pageNum);
        } else {
            call = getRequestApi().requestFanUserBean(userId, pageNum);
        }
        autoClose(call);
        if (call == null)
            return;
        call.enqueue(new Callback<FindInfo>() {
            @Override
            public void onResponse(Call<FindInfo> call, Response<FindInfo> response) {
                updateLoadStatus(LOAD_SUCCESS);
                if (response.isSuccessful()) {
                    FindInfo body = response.body();
                    if ("Success!".equals(body.getMsg())) {
                        questionList = body.getQuestionList();
                        if (questionList.size() != 0) {
                            if (type == TYPE_REFRESH) {
                                refreshDatas(TYPE_REFRESH);
                                xRefreshView.stopRefresh();
                            } else {
                                refreshDatas(TYPE_MORE);
                                xRefreshView.stopLoadMore();
                            }
                        } else {
                            if (type == TYPE_REFRESH) {
                                xRefreshView.stopRefresh();
                                updateLoadStatus(LOAD_NO_DATA);
                                return;
                            } else {
                                xRefreshView.stopLoadMore();
                            }
                            ToastUtil.showToastShort("没有更多");
                        }
                    } else {
                        ToastUtil.showToastShort("数据异常");
                        xRefreshView.stopRefresh();
                        xRefreshView.stopLoadMore();
                        updateLoadStatus(LOAD_NO_DATA);
                    }
                }
            }

            @Override
            public void onFailure(Call<FindInfo> call, Throwable t) {
                ToastUtil.showToastShort("网络异常");
            }
        });
    }

}
